package com.graduate.utils;

import com.alibaba.fastjson.JSONObject;
import com.graduate.exception.BaseErrorInfoInterface;
import com.graduate.exception.ExceptionEnum;

public class RespBody {
    /**
     * 响应代码
     */
    private String respCode;

    /**
     * 响应消息
     */
    private String respMsg;

    public String getRespCode() {
        return respCode;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }

    public String getRespMsg() {
        return respMsg;
    }

    public void setRespMsg(String respMsg) {
        this.respMsg = respMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 响应结果
     */
    private Object data;

    public RespBody() {
    }

    public RespBody(BaseErrorInfoInterface errorInfo) {
        this.respCode = errorInfo.getRespCode();
        this.respMsg = errorInfo.getRespMsg();
    }




    /**
     * 成功
     *
     * @return
     */
    public static RespBody success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static RespBody success(Object data) {
        RespBody rb = new RespBody();
        rb.setRespCode(ExceptionEnum.SUCCESS.getRespCode());
        rb.setRespMsg(ExceptionEnum.SUCCESS.getRespMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static RespBody error(BaseErrorInfoInterface errorInfo) {
        RespBody rb = new RespBody();
        rb.setRespCode(errorInfo.getRespCode());
        rb.setRespMsg(errorInfo.getRespMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static RespBody error(String respCode, String respMsg) {
        RespBody rb = new RespBody();
        rb.setRespCode(respCode);
        rb.setRespMsg(respMsg);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static RespBody error( String message) {
        RespBody rb = new RespBody();
        rb.setRespCode("-1");
        rb.setRespMsg(message);
        rb.setData(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }


}
